package org.ydeity.lucene.test.service;

import org.ydeity.lucene.test.entity.POI;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Service {

    public static List<POI> listAll() {
        List<POI> poiList = new ArrayList<>();

        Connection conn = null;
        PreparedStatement preStatement = null;
        ResultSet resultSet = null;

        try {
            // 加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            // 创建数据库连接对象
            conn = DriverManager.getConnection(
                    "jdbc:mysql://172.16.5.18:3306/wlxycx?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=Asia/Shanghai",
                    "wlxycx",
                    "rhQCFQ4i2v");

            // 定义查询SQL
            String sql = "select * from lj_sence";
            // 创建Statement语句对象
            preStatement = conn.prepareStatement(sql);
            // 执行语句, 得到结果集
            resultSet = preStatement.executeQuery();

            // 处理结果集
            while (resultSet.next()) {
                POI poi = new POI();
                poi.setId(Long.valueOf(resultSet.getString("Id")));
                poi.setName(resultSet.getString("name"));
                poi.setExplainPoints(resultSet.getString("explain_points"));
                poi.setLevel(resultSet.getString("level"));
                poi.setType(resultSet.getString("type"));
                poi.setIndoorScene(resultSet.getString("indoor_scene"));
                poi.setLon(Double.valueOf(resultSet.getString("lon")));
                poi.setLat(Double.valueOf(resultSet.getString("lat")));

                poi.setLocation(resultSet.getString("lat") + "," + resultSet.getString("lon"));

                poi.setState(resultSet.getString("state"));
                poi.setCountry(resultSet.getString("country"));
                poi.setProvince(resultSet.getString("province"));
                poi.setCity(resultSet.getString("city"));
                poi.setRegion(resultSet.getString("region"));
                poi.setAddress(resultSet.getString("address"));
                poi.setAppStatus(resultSet.getString("app_status"));

                poi.setStatus((short) 1);
                poi.setShortStatus((short) 1);
                poi.setPoiType(1);
                poi.setOpen(false);
                poi.setOpenStatus(true);
                poi.setNum(100);
                poi.setBigDecimal(BigDecimal.valueOf(18));
                poi.setDate(new Date());
                poi.setLocalDateTime(LocalDateTime.now());
                poi.setLocalDate(LocalDateTime.now().toLocalDate());

                // 将查询到的结果添加到list中
                poiList.add(poi);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 释放资源
            try {
                if (null != conn) conn.close();
                if (null != preStatement) preStatement.close();
                if (null != resultSet) resultSet.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return poiList;
    }

    public static void main(String[] args) {
        List<POI> pOis = Service.listAll();

        System.out.println(pOis.size());
    }
}